package com.wgz.ssm.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgz.ssm.bean.Employee;
import com.wgz.ssm.bean.EmployeeExample;
import com.wgz.ssm.bean.R;
import com.wgz.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author wgz
 * Created by admin on 2021/3/30
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public EmployeeService employeeService;

    @GetMapping("/list")
    public String list(){
        return "emp/list";
    }

    @PostMapping("/emplist")
    @ResponseBody
    public R emplist(@RequestBody(required = false) com.wgz.ssm.bean.vo.EmployeeVo employeeVo ) throws JsonProcessingException, ParseException {


        // 查询
        PageHelper.startPage(employeeVo.getPage(),employeeVo.getLimit());
//        List<Employee> list = employeeService.findAllEmp();
        //构造查询条件
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        if (employeeVo.getLastName() != null && !employeeVo.getLastName().equals("") ){
            criteria.andLastNameLike("%"+employeeVo.getLastName()+"%");
        }
        //开始时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (employeeVo.getStart() != null &&  !employeeVo.getStart().equals("")){
            Date parse = null;
            try {
                parse = sdf.parse(employeeVo.getStart());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            criteria.andDateGreaterThanOrEqualTo(parse);
        }
        //结束时间
        if (employeeVo.getEnd() != null && !employeeVo.getEnd().equals("")){
            Date parse = sdf.parse(employeeVo.getEnd());

            criteria.andDateGreaterThanOrEqualTo(parse);
        }

        List<Employee> list = employeeService.findAllEmp(employeeExample);

        // pageINfo
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        // 总记录
//        Map<String,Object> map = new HashMap<String, Object>();
//        map.put("code",0);
//        map.put("msg","加载成功");
//        map.put("count",pageInfo.getTotal());
//        map.put("data",pageInfo.getList());
        R r = new R();
        r.setCode(0);
        r.setMsg("");
        r.setCount(pageInfo.getTotal());
        r.setData(pageInfo.getList());
        // 转字符串
//        ObjectMapper mapper = new ObjectMapper();
//        String data = mapper.writeValueAsString(map);

        return r;
    }

    @PostMapping("/insert")
    @ResponseBody
    public String insert( @RequestBody Employee employee ){

        int i =  employeeService.insert(employee);

        return i > 0 ? "true" : "false" ;
    }


    @PostMapping("/update")
    @ResponseBody
    public String update( @RequestBody Employee employee ){

        int i =  employeeService.update(employee);

        return i > 0 ? "true" : "false" ;
    }

}
